<?php
abstract class Pageley_Test_AbstractResult {
	private $testResults;
	private $name;
	
	public function __construct($name) {
		$this->name = $name;
		$this->testResults = new Pageley_Util_ArrayMap();
	}
	
	public function getSuccess() {
		return $this->getSuccessCount() == $this->getTestCount();
	}
	
	public abstract function getTestCount();

	public function getFailureCount() {
		return $this->countResults(Pageley_Test_ResultType::FAILURE);
	}
	
	public function getErrorCount() {
		return $this->countResults(Pageley_Test_ResultType::ERROR);
	}

	public function getSuccessCount() {
		return $this->countResults(Pageley_Test_ResultType::SUCCESS);
	}
	
	public abstract function countResults($type);
	
	protected abstract function getTestLevel();

	
	public function getTestResults() {
		return $this->testResults;
	}
	
	public function addTestResult($testResult) {
		$this->testResults->add($testResult);
	}
	
	public function getName() {
		return $this->name;
	}	
	
	public function __toString() {
		$message = "";
		if($this->getTestCount() > 0) {
			$message .= $this->getTestLevel() . " '" . $this->name . "': " .
				$this->getSuccessCount() . " succeeded, " . 
				$this->getFailureCount() . " failed, " . 
				$this->getErrorCount() . " errors, " . 
				$this->getTestCount() . " tests total\n";
			if($this->getFailureCount() > 0 || $this->getErrorCount() > 0) {
				foreach($this->getTestResults() as $testResult) {
					if($testResult->getFailureCount() > 0 || $testResult->getErrorCount() > 0) {
						$message .= " > " . $testResult;
					}
				}
			}
		} else {
			$message .= $this->getTestLevel() . " '" . $this->name . "': no tests to run\n";
		}
		return $message;
	}
}